import * as giteeApi from '@/data/api/gitee'
import {useAccountStore} from "@/data/stores/account";
import type {GiteeFile} from "@/data/entity/GiteeFile";
import {ResultCode} from "@/data/constants/ResultCode";
import {DEFAULT_REPO_NAME} from "@/data/constants/Constants";

const accountStore = useAccountStore()
function validateToken(){
    const giteeAccount = accountStore.getGitee()
    return new Promise((resolve, reject)=>{
        giteeApi.getUser(giteeAccount.access_token).then((response)=>{
            giteeAccount.owner = response.data.login
            giteeAccount.avatar_url = response.data.avatar_url
            giteeAccount.html_url = response.data.html_url
            giteeAccount.auto_save = true
            giteeAccount.repo = DEFAULT_REPO_NAME
            giteeApi.createRepo(giteeAccount.access_token, giteeAccount.repo).then(()=>{
                accountStore.setGitee(giteeAccount)
                resolve(response)
            })
        }).catch(err=>{
            reject(err)
        })
    })
}

function getFile(fileName:string):Promise<GiteeFile>{
    const giteeAccount = accountStore.getGitee()
    return new Promise((resolve, reject)=>{
        giteeApi.getFile(
            giteeAccount.access_token,
            giteeAccount.owner,
            giteeAccount.repo,
            fileName
        ).then((response)=>{
            if (response.data instanceof Array){
                if (response.data.length != 0){
                    reject(ResultCode.FILE_IS_FOLDER)
                } else {
                    reject(ResultCode.FILE_NOT_EXIST)
                }
            } else {
                resolve(response.data)
            }
        })
    })
}

function getFolder(fileName:string):Promise<GiteeFile[]>{
    const giteeAccount = accountStore.getGitee()
    return new Promise((resolve, reject)=>{
        giteeApi.getFile(
            giteeAccount.access_token,
            giteeAccount.owner,
            giteeAccount.repo,
            fileName
        ).then((response)=>{
            if (response.data instanceof Array){
                resolve(response.data)
            } else {
                reject(ResultCode.NOT_A_FOLDER)
            }
        })
    })
}

function createFile(path:string, content:string, message:string){
    const giteeAccount = accountStore.getGitee()
    return new Promise((resolve, reject) => {
        giteeApi.postFile(
            giteeAccount.access_token,
            giteeAccount.owner,
            giteeAccount.repo,
            path,
            content,
            GiteeOperation.CREATE + message
        ).then(rs => {
            resolve(rs)
        }).catch(err => {
            reject(err)
        })
    })
}

/**
 * 更新一个文件，若文件不存在则新建
 * @param fileName
 * @param content
 * @param message
 */
function updateFile(fileName:string, content:string, message:string){
    const giteeAccount = accountStore.getGitee()
    return new Promise((resolve, reject) => {
        getFile(fileName).then((rs) => {
            giteeApi.putFile(
                giteeAccount.access_token,
                giteeAccount.owner,
                giteeAccount.repo,
                fileName,
                content,
                GiteeOperation.UPDATE + message,
                rs.sha
            ).then(rs => {
                resolve(rs)
            }).catch(err => {
                reject(err)
            })
        }).catch(err => {
            if (err == ResultCode.FILE_NOT_EXIST){
                createFile(
                    fileName,
                    content,
                    message
                ).then(rs => {
                    resolve(rs)
                }).catch(err => {
                    reject(err)
                })
            } else {
                reject(err)
            }
        })

    })
}

const GiteeOperation = {
    CREATE: 'create: ',
    UPDATE: 'update: ',
    DELETE: 'delete: '
}
export { validateToken, updateFile, createFile, getFile, getFolder }